package com.tekj.cxqcb.view.Module.activity.Order;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.library.flowlayout.FlowLayoutManager;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.OrderListBean;
import com.tekj.cxqcb.view.Module.adapter.CarOrderAdapter;
import com.tekj.cxqcb.view.Module.adapter.Screen5Adapter;
import com.tekj.cxqcb.view.Module.bean.ScreenBean;
import com.tekj.cxqcb.view.Module.decoration.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.PUB;
import es.dmoral.toasty.Toasty;

/**
 * 买车全部订单
 */
public class CarOrderListActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.srl_main)
    SwipeRefreshLayout srlMain;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.ll_center)
    LinearLayout llCenter;

    CarOrderAdapter carOrderAdapter;
    Screen5Adapter screen5Adapter;
    BusinessDao businessDao;
    int page = 1;
    String type = "";
    String[] typeNames = {"全部订单", "待审批", "待提交资料", "待付款", "待客户提车", "订单完成"};
    String search;

    @Override
    public void init() {
        businessDao = new BusinessDao(activity, this);

        carOrderAdapter = new CarOrderAdapter(null);
        rvMain.setHasFixedSize(true);
        rvMain.setLayoutManager(new LinearLayoutManager(activity));
        rvMain.addItemDecoration(new SpacesItemDecoration(15));
        rvMain.setAdapter(carOrderAdapter);

        carOrderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                activity.startActivity(new Intent(activity, CarOrderDetailsActivity.class)
                        .putExtra("id", carOrderAdapter.getData().get(position).getId())
                );
            }
        });

        carOrderAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
            }
        }, rvMain);
        carOrderAdapter.setEmptyView(R.layout.rv_empty);

        List<ScreenBean> list1 = new ArrayList<>();
        for (int i = 0; i < typeNames.length; i++) {
            list1.add(new ScreenBean(typeNames[i], i + "", ""));
        }
        screen5Adapter = new Screen5Adapter(list1);

        screen5Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                screen5Adapter.setCheckItem(position);
                type = screen5Adapter.getData().get(position).getId();
                tvTitle.setText(screen5Adapter.getData().get(position).getName());
                search = null;
                etSearch.setText("");
                page = 1;
                getData();
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }

            }
        });

        srlMain.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索的时候隐藏软键盘
                    hideKeyboard(etSearch);
                    // 在这里写搜索的操作,一般都是网络请求数据
                    search = etSearch.getText().toString().trim();
                    page = 1;
                    getData();
                    return true;
                }
                return false;
            }
        });

        getData();

    }

    void getData() {
        businessDao.DoGetOrderListByStatxusAndSalesId(type, MainBActivity.User.getUserId(), page, search);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_car_order_list;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 购车订单列表:
                OrderListBean bean = (OrderListBean) bindingViewBean.getBean();
                if (bean.getCode().equals("0000000")) {
                    if (page == 1) {
                        carOrderAdapter.setNewData(bean.getData().getList());
                    } else {
                        carOrderAdapter.addData(bean.getData().getList());
                    }
                    carOrderAdapter.loadMoreComplete();
                    if (bean.getData().getList().size() < 10) {
                        carOrderAdapter.loadMoreEnd();
                    }
                }
                if (srlMain != null) {
                    srlMain.setRefreshing(false);
                }
                break;
        }
    }

    private PopupWindow popupWindow;
    private View popupView;
    private TranslateAnimation animation;

    private void showPop(RecyclerView.Adapter adapter) {
        popupView = View.inflate(activity, R.layout.pop_list, null);
        popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);


        animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, -1, Animation.RELATIVE_TO_PARENT, 0);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(200);

        View miss = popupView.findViewById(R.id.view_miss);
        miss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        RecyclerView rvScreen = popupView.findViewById(R.id.rv_main);
        rvScreen.setLayoutManager(new FlowLayoutManager());
        rvScreen.setHasFixedSize(true);
        rvScreen.setAdapter(adapter);
        rvScreen.addItemDecoration(new SpacesItemDecoration(5));


        popupWindow.showAsDropDown(rlTitleBg);
        popupView.startAnimation(animation);
    }

    @OnClick({R.id.ll_back, R.id.ll_center, R.id.iv_r})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:

                if (llSearch.getVisibility() == View.VISIBLE) {
                    dismissS();
                } else {
                    finish();
                }
                break;
            case R.id.ll_center:
                showPop(screen5Adapter);
                break;
            case R.id.iv_r:
                showS();
                break;
        }
    }

    void showS() {
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(imgBack, "rotation", 180f);
        objectAnimator3.setDuration(500);


        ScaleAnimation scaleAnimation =
                new ScaleAnimation(0.2f, 1, 1, 1,
                        Animation.RELATIVE_TO_PARENT, 1f,
                        Animation.RELATIVE_TO_PARENT, 1f);
        scaleAnimation.setDuration(500);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                llCenter.setVisibility(View.GONE);
                llRight.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        objectAnimator3.start();
        llSearch.setVisibility(View.VISIBLE);
        llSearch.startAnimation(scaleAnimation);
    }

    void dismissS() {
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(imgBack, "rotation", 0f);
        objectAnimator3.setDuration(500);


        ScaleAnimation scaleAnimation =
                new ScaleAnimation(1f, 0.2f, 1, 1,
                        Animation.RELATIVE_TO_PARENT, 1f,
                        Animation.RELATIVE_TO_PARENT, 1f);
        scaleAnimation.setDuration(500);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                llCenter.setVisibility(View.VISIBLE);
                llSearch.setVisibility(View.GONE);
                llRight.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        objectAnimator3.start();
        llSearch.startAnimation(scaleAnimation);
    }

    /**
     * 隐藏软键盘
     *
     * @param view :一般为EditText
     */
    public void hideKeyboard(View view) {
        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                if (llSearch.getVisibility() == View.VISIBLE) {
                    dismissS();
                } else {
                    finish();
                }
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
}
